package com.zhf.controller;

import com.github.pagehelper.PageInfo;
import com.zhf.domain.ResponseResult;
import com.zhf.domain.Role;
import com.zhf.domain.User;
import com.zhf.domain.UserVo;
import com.zhf.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

/**
 * @Author: ZhaoHuanfeng
 * @Date: 2020/12/4 22:19
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /*
    用户分页&多条件查询
     */
    @RequestMapping("/findAllUserByPage")
    public ResponseResult findAllUserByPage(@RequestBody UserVo userVo){
        PageInfo pageInfo = userService.findAllUserByPage(userVo);

        return  new ResponseResult(true,200,"分页多条件查询成功",pageInfo);
    }
    
    /*
    用户登录
     */
    @RequestMapping("/login")
    public ResponseResult login(User user, HttpServletRequest request) throws Exception {
        User user1 = userService.login(user);
        if (user1!=null){
            //保存用户id及access_token到session中
            HttpSession session = request.getSession();
            String token = UUID.randomUUID().toString();
            session.setAttribute("access_token", token);
            session.setAttribute("user_id",user1.getId());
            //将查询到的信息响应到前台
            HashMap<String, Object> map = new HashMap<>();
            map.put("access_token", token);
            map.put("user_id", user1.getId());
            ResponseResult result = new ResponseResult(true, 200, "登录成功", map);
            return result;
        }else {
            ResponseResult result = new ResponseResult(true, 400, "用户名密码错误", null);
            return result;
        }
    }

    /*
    分配角色（回显）
     */
    @RequestMapping("/findUserRoleById")
    public ResponseResult findUserRelationRoleById(Integer id){
        List<Role> roleList = userService.findUserRelationRoleById(id);
        ResponseResult result = new ResponseResult(true, 200, "分配角色回显成功", roleList);
        return result;
    }

    /*
    分配角色
     */
    @RequestMapping("/userContextRole")
    public ResponseResult userContextRole(@RequestBody UserVo userVo){
        userService.userContextRole(userVo);
        ResponseResult result = new ResponseResult(true, 200, "分配角色成功", null);
        return result;
    }

    /*
    获取用户权限，进行菜单动态展示
     */
    @RequestMapping("/getUserPermissions")
    public ResponseResult getUserPermissions(HttpServletRequest request){
        //获取请求头的token
        String header = request.getHeader("Authorization");
        //获取session中的token
        String access_token = (String) request.getSession().getAttribute("access_token");
        //判断token是否一致
        if(header.equals(access_token)){
            //获取用户id
            Integer user_id = (Integer) request.getSession().getAttribute("user_id");
            //调用service进行菜单信息查询
            ResponseResult result = userService.getUserPermissions(user_id);
            return result;
        }else{
            ResponseResult result = new ResponseResult(false, 400, "获取菜单信息失败", null);
            return result;
        }
    }
}
